package com.jaydenxiao.common.photopicker;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.jaydenxiao.common.R;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.basebean.ItemBean;
import com.jaydenxiao.common.basebean.MsgEvent;
import com.jaydenxiao.common.commonwidget.NoDoubleClickUtils;
import com.jaydenxiao.common.databinding.ActivityRecycleViewNoPageBinding;
import com.jaydenxiao.common.drag_listener.RecyclerItemClickListener;
import com.jaydenxiao.common.photopicker.adapter.PhotoAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;


/**
 * Created by hasee on 2017/10/25.
 */

public class PhotoChoseActivity extends BaseActivity<ActivityRecycleViewNoPageBinding> {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_recycle_view_no_page;
    }

    private ItemBean itemBean;
    private PhotoAdapter photoAdapter;
    public static final String PHOTO_LIST="photo_list";

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        Log.e("intentNum","进来了一次");

        dataBinding.backTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NoDoubleClickUtils.isDoubleClick()){
                    return;
                }
                itemBean.setValue2("新图片");
                itemBean.setValue("已选择"+itemBean.getImgUrlList().size()+"张照片");
                EventBus.getDefault().post(new MsgEvent(MsgEvent.ITEM_BEAN_UPDATE_EVENT,itemBean));
                finish();
            }
        });

        itemBean=getIntent().getParcelableExtra(PHOTO_LIST);
        photoAdapter = new PhotoAdapter(this,itemBean.getImgUrlList());
        dataBinding.RV.setLayoutManager(new StaggeredGridLayoutManager(6, OrientationHelper.VERTICAL));
        dataBinding.RV.setAdapter(photoAdapter);

        dataBinding.RV.addOnItemTouchListener(new RecyclerItemClickListener(this,
                new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        if (photoAdapter.getItemViewType(position) == PhotoAdapter.TYPE_ADD) {
                            PhotoPicker.builder()
                                    .setPhotoCount(PhotoAdapter.MAX)
                                    .setShowCamera(true)
                                    .setPreviewEnabled(false)
                                    .setSelected(itemBean.getImgUrlList())
                                    .start(PhotoChoseActivity.this);
                        } else {
                            PhotoPreview.builder()
                                    .setPhotos(itemBean.getImgUrlList())
                                    .setCurrentItem(position)
                                    .start(PhotoChoseActivity.this);
                        }
                    }
                }));

    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                itemBean.getImgUrlList().clear();
                itemBean.getImgUrlList().addAll(photos);
                photoAdapter.notifyDataSetChanged();
            }
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            itemBean.setValue2("新图片");
            itemBean.setValue("已选择"+itemBean.getImgUrlList().size()+"张照片");
            EventBus.getDefault().post(new MsgEvent(MsgEvent.ITEM_BEAN_UPDATE_EVENT,itemBean));
            finish();
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }




}
